//
//  visitorLoginView.swift
//  CYMWeiBo-20
//
//  Created by apple on 15/12/13.
//  Copyright © 2015年 IT 小码农. All rights reserved.
//

import UIKit

//定义协议
protocol visitorLoginViewDelegate: NSObjectProtocol {

    //声明协议方法
    //用户将要登陆
    func userWillLogin()
    
    //用户将要注册
    func userWillRegister()

}

class visitorLoginView: UIView {
    
    //声明代理 用 weak 属性关键字, Swift 中默认是 strong
    weak var visitroViewDelegate: visitorLoginViewDelegate?
    
    
    
    //对外提供设置访客视图信息的方法
    func setUpInfo(tipText: String,imageName: String?) {
    tipLabel.text = tipText
        if let name = imageName {
        //如果不为空 就不是首页  圆圈
            circleView.image = UIImage(named: name)
            
            //隐藏大房子
            largeIcon.hidden = true
            //将circleView移动到视图最顶层
            bringSubviewToFront(circleView)
        }else {
        //是首页 就开始做动画
            startAnimation()
        }
    }

    //开始动画方法
    private func startAnimation() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
    
        //设置动画属性 repeat重复-->锐皮特
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        anim.toValue = 2 * M_PI
        
        //当动画完毕或者页面失去活跃状态动画不移除图层
        anim.removedOnCompletion = false
        
        //添加动画到图层
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    
    //初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置所有的子视图
    private func setUpUI() {
        
        //添加圆圈
        addSubview(circleView)
        addSubview(backView)
        //添加大图(房子)
        addSubview(largeIcon)
        //文字标签添加
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        
        //使用自动布局 Constraint限制-->康斯吹的
        /**
        *  返回一个自动布局约束对象
        *
        *  @param  item         需要添加约束的对象
        *  @param  attribute  添加约束的对象的需要约束的属性
        *  @param  relatedBy   约束的关系 一般用 equal 表示精准约束
        *  @param  toItem         相对约束的对象
        *  @param  attribute 相对约束的对象的约束
        *  @param  multiplier 乘积值 一般1
        *  @param constant 约束值
        */
        
        for subView in subviews {
        //在使用手码添加约束 需要屏蔽 frame 布局
            subView.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
        //把约束添加到父视图
       addConstraint(NSLayoutConstraint(item: largeIcon, attribute:  .CenterX, relatedBy: .Equal, toItem: self, attribute:  .CenterX, multiplier: 1, constant: 0))
        
        //添加第二个约束
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute:  .CenterY, relatedBy: .Equal, toItem: self, attribute:  .CenterY, multiplier: 1, constant: 0))
        
        
        //添加圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute:  .CenterX, relatedBy:  .Equal, toItem: largeIcon, attribute:  .CenterX, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: circleView, attribute:  .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute:  .CenterY, multiplier: 1, constant: 0))
        
        //文字文本标签  确定 X
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute:  .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        //确定 Y
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        
        //确定 W
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        
        //确定 H
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
     
        //按钮的约束  左边确定 X
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        
        //上边确定 Y
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        
        //确定宽
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        //确定高
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //注册按钮
        //确定 Y
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        
        //确定 X
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        
        //确定宽
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))

        //确定高
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置背景视图的约束,可视化的布局语言  水平方向距离0
        /**
        metrics 约束数值字典
        [String: 数值]
        */
       addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        
        //垂直 相对于注册按钮
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["backView": backView,"regBtn": registerBtn]))
        
        
        //设置视图的背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加两个按钮的点击事件
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        
    }

    //点击事件
    @objc private func registerBtnDidClick() {
    
        //代理调用协议方法 如果为 nil 就不会执行方法 所以要用可选项
        visitroViewDelegate?.userWillRegister()
    }
    
    @objc private func loginBtnDidClick() {
    //判断代理是否为空 && 是否响应方法 userWillLogin? 表示判断是否可以响应方法
        visitroViewDelegate?.userWillLogin()
        
    }
    
    
    
    //懒加载所有的子视图 私人的 large 大的
    private lazy var largeIcon: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    
    //背景视图
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //圆圈 circle-->seGOU
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    private lazy var tipLabel: UILabel = {
       let l = UILabel()

    l.text = "关注一些人,回这里看看有什么惊喜"
        //灰色
       l.textColor = UIColor.darkGrayColor()
        
        l.textAlignment = .Center
        l.font = UIFont.systemFontOfSize(14)
        l.numberOfLines = 0
        l.sizeToFit()
        
        return l
    }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
    
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.setTitle("注册", forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        btn.setTitleColor(themeColor, forState: .Normal)
        
        btn.sizeToFit()
        
        return btn
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
    
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.setTitle("登录", forState: .Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        btn.sizeToFit()
        
        
    return btn
    }()
    
    
}
